/*
 * @Author: 吴世扬 18368095041@163.com
 * @Date: 2024-09-29 11:52:37
 * @LastEditors: 吴世扬 18368095041@163.com
 * @LastEditTime: 2024-09-29 11:59:08
 * @FilePath: /react18-vite-admin/src/service/useStateRef.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { useState, useRef } from 'react';

export const useStateRef = <T>(initVal: T) => {
    const [state, setState] = useState(initVal);

    const ref = useRef(initVal);

    const setStateRef = (val: T) => {
        ref.current = val;
        setState(ref.current);
    };

    return [state, ref, setStateRef] as [T, typeof ref, typeof setStateRef];
};
